package com.xb.loan.admin.service.system.impl;

import com.xb.loan.admin.dao.system.OssUserLogDao;
import com.xb.loan.admin.service.system.OssUserLogService;
import com.xb.loan.admin.util.AbstractUtilService;
import com.xb.loan.log.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Service("ossUserLogService")
public class OssUserLogServiceImpl extends AbstractUtilService implements OssUserLogService {
	@Autowired
	private OssUserLogDao ossUserLogDao;
	@Override
	public List<Map<String, Object>> queryUserLogList(String userId, String logModel, String endTime, String startTime,
			int pageNum, int numPerPageJson) throws BizException {
		Map<String,Object> params = new HashMap<String,Object>();
        params.put("tableId", getTableId(userId));
        params.put("userId", userId);
        params.put("logModule", logModel);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("pageNum", pageNum);
        params.put("numPerPage", numPerPageJson);
        List<Map<String, Object>> list = ossUserLogDao.queryUserLogList(params);
        
        for(Map<String, Object> map : list){
        	String logContent = String.valueOf(map.get("LOG_CONTENT"));
        	String loginName = String.valueOf(map.get("LOGIN_NAME"));
        	if("is_oss_opertor".equals(loginName)){
        		map.put("LOGIN_NAME", getLoginName(logContent));
        	}
        }
        
        return list;
	}
	private String getLoginName(String logContent){
		String regex = "\\操作人：(.*?)\\,";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(logContent);
		while(m.find()){
			return m.group(1);
		}
		return "";
	}

	@Override
	public int queryUserLogCount(String userId, String logModel, String endTime, String startTime) {
		Map<String,Object> params = new HashMap<String,Object>();
        params.put("tableId", getTableId(userId));
        params.put("userId", userId);
        params.put("logModule", logModel);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        int total = ossUserLogDao.queryUserLogCount(params);
        return total;
	}
	public static void main(String[] args) {
		String str = "操作人:xxx；操作信息：手机号码13911111变更为18611111";
		String regex = "\\操作人:(.*?)\\；";
//		String regex = "\\[([^\\]]+)\\]";
//		String str = ":您好]，abcdefg，[abc]";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(str);
		while(m.find()){
			System.out.println(m.group(1));
			System.out.println(m.group());
		}
	}
}
